//
//  HMMainViewController.swift
//  Weibo
//
//  Created by Apple on 2017/7/21.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit

class HMMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //  添加子控制器1
//        viewControllers = [xx]
        //  添加子控制器2
//        addChildViewController(xx)
        
        let hmTabBar = HMTabBar()
        //  设置代理
        hmTabBar.hmDelegate = self
        //  设置闭包
        hmTabBar.callBack = { [unowned self] in
            print("我是闭包回调过来的")
            print(self)
        }
        //  kvc给只读属性赋值，本质是给私有成员变量赋值
        self.setValue(hmTabBar, forKey: "tabBar")
        
        
        addChildViewController(childController: HMHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(childController: HMMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: HMDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(childController: HMMeTableViewController(), title: "我的", imageName: "tabbar_profile")
    }

    //  重载添加子视图控制器方法
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        //  设置tabbar的tintColor, 越早设置越好，一般在AppDelegate统一设置
//        UITabBar.appearance().tintColor = UIColor.orange
        
//        childController.tabBarItem.title = title
        childController.tabBarItem.image = UIImage(named: imageName)?.withRenderingMode(.alwaysOriginal)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
//        childController.navigationItem.title = title
        //  如果文字一样，设置控制器的标题
        childController.title = title
        //  设置选中文字的颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //  设置文字大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        let nav = HMNavigationController(rootViewController: childController)
        //  添加子视图控制器
        addChildViewController(nav)
        
        
    }
   

}

//  MARK:   --HMTabBarDelegate
extension HMMainViewController: HMTabBarDelegate {
    func clickComposeButtonOperation() {
        print("我是代理对象调用过来的")
    }
}
